<?php

class Core_Data_SQL_Structure_Database {

    private $arrTables = null;

    public $Connection;
    private $strName;

    function __construct ( $Connection, $strName ) {
        $this->Connection = $Connection;
        $this->strName = $strName;
    }

    /**
     * Gets the name of the table.
     *
     * @return string
     */
    function __toString () {
        return $this->strName;
    }

    /**
     * Removes an entire table from the database.
     * 
     * @param string $strTableName 
     */
    public function removeTable ( $strTableName ) {
        // TODO: Throw an exception if $strTableName is not a table.
        $strQuery = "DROP TABLE `" . $strTableName . "`";
        $this->Connection->query( $strQuery );
    }

    public function tables () {

        if ( $this->arrTables === null ) {
            
            $strQuery = "SHOW TABLES";
            $arrResult = $this->Connection->fetch( $strQuery );
            $strIndex = "Tables_in_" . $this->__toString();

            if ( $arrResult ) {
                foreach ( $arrResult as $arrTable ) {
                    $strName = $arrTable[$strIndex];
                    $objTable = new Core_Data_SQL_Structure_Table ( $this, $strName );
                    $this->arrTables[] = $objTable;
                }
            }

        }

        return (object) $this->arrTables;

    }

    /**
     * Wipes the entire database.
     */
    public function wipe () {
        foreach ( $this->tables() as $objTable ) {
            $this->removeTable( (string) $objTable );
        }
    }

}
?>
